
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package be.ac.ua.pats.thesisrobotica.robot.RR.sensors.listeners;

//~--- non-JDK imports --------------------------------------------------------

import be.ac.ua.pats.thesisrobotica.robot.RR.sensors.utils.InterrupterThread;

import lejos.nxt.SensorPort;

/**
 *
 * @author Admin
 */
public abstract class TouchSensorListener extends SensorListener {
    final lejos.nxt.TouchSensor touchSensor;

    public TouchSensorListener(SensorPort port, long interval, long threshold) {
        super(port, interval, threshold);

        touchSensor       = new lejos.nxt.TouchSensor(port);
        /**
        interrupterThread = new InterrupterThread(this) {
            @Override
            public void start() {
                while (isBlocked()) {}

                Boolean currentPressed = touchSensor.isPressed();

                while (currentPressed == touchSensor.isPressed()) {}

                interruptThread();
            }
        };
         * */
        this.start();
    }

    public abstract void stateChanged();

    @Override
    public void run() {
        while (true) {
            try {
                interrupterThread.start();
                interrupterThread.setBlocked(false);
                Thread.sleep(interval);
                interrupterThread.setBlocked(true);
                stateChanged();
            } catch (InterruptedException ex) {}
        }
    }
}
